package network;

import android.app.Dialog;
import android.content.Context;

import com.google.gson.Gson;
import com.loopj.android.http.TextHttpResponseHandler;
import com.orhanobut.logger.Logger;


import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import app.MyApplication;
import Utils.MyToast;

/**
 * Created by zx on 2017/3/16.
 */

public abstract class ObjectHttpResponseHandler<T> extends TextHttpResponseHandler {


    private  Class mClass;
    private Context mContext;
    Dialog mWaitDialog=MyApplication.getWatiDialog();

    protected ObjectHttpResponseHandler(Context context) {

        this.mContext = context;
    }

    protected ObjectHttpResponseHandler(Context context, Class mClass) {
        this.mClass = mClass;
        this.mContext = context;
    }

    @Override
    public void onFailure(int statusCode, org.apache.http.Header[] headers, String responseString, Throwable throwable) {
        Failure(statusCode, headers, responseString, throwable);

        Logger.e("NET--------",throwable);
    }

    @Override
    public void onSuccess(int statusCode, org.apache.http.Header[] headers, String responseString) {

        try {
            JSONObject mJSONObject = new JSONObject(responseString);
            Gson mGson = new Gson();
            Success(statusCode, headers, mGson.fromJson(responseString, mClass));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public abstract void Success(int i, Header[] headers, Object object);


    public  void Failure(int i, Header[] headers, String s, Throwable throwable){

    }
}
